﻿@model SIG.Model.ProductPhoto
@{
    ViewBag.Title = "产品图片";
    ViewBag.PageTitle = ViewBag.Title + "_产品_内容";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

@section Header{
<link href="~/Areas/Admin/Content/Plugins/lightbox/ekko-lightbox.min.css" rel="stylesheet" />
<script src="~/Areas/Admin/Content/Plugins/ckfinder/ckfinder.js"></script>
}

@section leftNav{
    @Html.Partial("_NavForContent")
}

<div class="toolbar">
    <a href="#" class="showmenu"><i class="glyphicon glyphicon-menu-hamburger"></i></a>
    <ol class="breadcrumb">
        <li><a href="@Url.Action("Index", "Home")"><i class="glyphicon glyphicon-home"></i> 控制面板</a></li>
        <li><a href="@Url.Action("Index")">产品</a></li>
        <li class="active">@ViewBag.Title</li>
    </ol>
</div>


<div class="main-content">
    <div class="panel panel-default">
        <header class="panel-heading panel-heading-tabs">
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="@Url.Action("Index")">产品列表</a></li>
                <li role="presentation"><a href="@Url.Action("Categories")">产品分类</a></li>
            </ul>
        </header>


        <div class="panel-body">
            <!-- gallery wrapper -->
            <div class="gallery-wrapper">
                <div class="page-header">
                    <h3><span class="glyphicon glyphicon-picture"></span> 产品组图</h3>
                </div>
                <div class="row gallery-row">

                    @foreach (var photo in (IEnumerable<ProductPhoto>) ViewBag.Photos)
                    {
                        <!-- single image -->
                        <div class="col-xs-3 img-container">
                            <div class="img-box">
                                <a href="@Url.Action("DeletePhoto", new {Id = photo.Id})" class="btn btn-xs btn-danger delete-item" title="删除"><i class="glyphicon glyphicon-trash"></i></a>
                                <img class="img-responsive" alt="picture" src="@photo.ImgURL"/>
                            </div>
                        </div>

                    }
                </div>
            </div>
            <!-- end gallery wrapper -->
            <hr/>
            <div class="no-gallery">
                <div class="page-header">

                    <h3><span class="glyphicon glyphicon-picture"></span> 上传图片</h3>

                </div>

            @using (Ajax.BeginForm("AddPhoto", Model, new AjaxOptions()
            {

                LoadingElementId = "saving",
                LoadingElementDuration = 2000,
                //   Confirm = "Are you sure you want to save this User?"
                OnSuccess = "updateSuccess"}, new { @class = "form-horizontal", role = "form" }))
                {

                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)
                
                    @Html.HiddenFor(model => model.ProductId)

                    <div class="form-group">
                        @Html.LabelFor(model => model.ImgURL, new { @class = "col-md-2 control-label" })
                        <div class="col-md-5">
                            <div class="input-group">
                                @Html.TextBoxFor(model => model.ImgURL, new { @class = "form-control", placeholder = ModelMetadata.FromLambdaExpression(x => x.ImgURL, ViewData).Watermark })
                                <span class="input-group-btn">
                                    <button class="btn btn-default" id="btnThumbnail" type="button">浏览…</button>
                                </span>
                            </div><!-- /input-group -->
                            <span class="help-block">图片尺寸：800x800 像素</span>
                        </div>
                        <div class="col-md-5">
                            @Html.ValidationMessageFor(model => model.ImgURL)
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.Importance, new { @class = "col-md-2 control-label" })
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.Importance, new { @class = "form-control", type = "number", placeholder = ModelMetadata.FromLambdaExpression(x => x.Importance, ViewData).Watermark })
                        </div>
                        <div class="col-md-5">
                            @Html.ValidationMessageFor(model => model.Importance)
                        </div>

                    </div>


                    <div class="form-group">
                        <div class="col-sm-offset-2 col-md-10">
                            <input type="submit" class="btn btn-primary" value="添加产品图片" />
                            <a href="@Url.Action("Index","Product")" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> 返回</a>
                        </div>
                    </div>

                }



            </div>
            <!-- end blank state -->
        </div>


    </div>
</div>

@section Footer {
    @Scripts.Render("~/bundles/jqueryval")
<script src="~/Areas/Admin/Content/Plugins/lightbox/ekko-lightbox.min.js"></script>
    <script>




        function SetThumbnail(fileUrl) {
            $('#ImgURL').val(fileUrl);
        }


           
        function updateSuccess(data) {

            //  console.log(data.Oicq);
            toastr.options = {
                "positionClass": "toast-bottom-full-width"
            }
            if (data) {
                toastr.success('产品图片已添加成功！', '@ViewBag.Title');
                setTimeout(function() {
                    location.reload();
                }, 1000);
            } else {
                toastr.error('产品图片添加失败！', '@ViewBag.Title');
            }


        }



        $(function () {
            $("#btnThumbnail").on("click", function () {
                var finder = new CKFinder();
                //  finder.basePath = '/plugins/ckfinder/';
                finder.selectActionFunction = SetThumbnail;
                finder.popup();
            });

            //确认框默认语言
            bootbox.setDefaults({
                locale: "zh_CN"
            });

            //当前菜单
            $('.mainmenu>li').eq(1).addClass('nav-open').find('.submenu li:nth-of-type(1)').children('a').addClass('active');


            //删除数据
            $('.delete-item').click(function (e) {
                e.preventDefault();

                var url = $(this).attr('href');
                var that = $(this);
                bootbox.confirm("你确定要删除此项吗？", function (result) {
                    /*DELETE*/
                    if (result) {
                        $.ajax({
                            url: url,
                            dataType: "json",
                            type: "DELETE",
                            contentType: 'application/json; charset=utf-8',
                            data: JSON.stringify({}),
                            async: true,
                            processData: false,
                            cache: false,
                            success: function (data) {
                                if (data) {
                                    toastr.success('产品图片已删除成功！', '删除产品图片');
                                    that.closest('.img-container').remove();
                                } else {
                                    toastr.error('产品图片删除失败！', '删除产品图片');
                                }
                            },
                            error: function (xhr) {
                                toastr.error('产品图片删除失败！', '删除产品图片');
                            }
                        });
                    }

                });


            });

        });
    </script>
}
